import { loadEnv } from 'vite'
import { buildLocalIconNames } from './assets/script/buildIcons'

let envScript = 'development'
if (['build', 'generate'].includes(process.env.npm_lifecycle_event ?? '')) {
    envScript = 'production'
}
const envData = loadEnv(envScript, '')
if (!envData.VITE_API_BASE_URL) {
    throw new Error('请先于 .env.production 文件内,配置生产环境的 VITE_API_BASE_URL')
}

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
    runtimeConfig: {
        public: {
            env: envData.VITE_ENV,
            apiBaseUrl: envData.VITE_API_BASE_URL,
        },
    },
    typescript: {
        shim: false,
    },
    modules: ['@vueuse/nuxt', '@unocss/nuxt', '@element-plus/nuxt', '@pinia/nuxt', '@pinia-plugin-persistedstate/nuxt', 'nuxt-icons'],
    imports: {
        dirs: ['stores'],
    },
    hooks: {
        'build:before': () => {
            buildLocalIconNames()
        },
    },
    vite: {
        resolve: {
            alias: {
                'vue-i18n': envData.VITE_ENV == 'production' ? 'vue-i18n/dist/vue-i18n.cjs.prod.js' : 'vue-i18n/dist/vue-i18n.cjs.js',
            },
        },
    },
    // 直接加载el的css以供随时使用 --el 开头的css类
    css: ['element-plus/dist/index.css', 'element-plus/theme-chalk/display.css', '~/assets/scss/index.scss', 'font-awesome/css/font-awesome.min.css'],
    vueuse: {
        ssrHandlers: true,
    },
    unocss: {
        uno: true,
        attributify: true,
        icons: {
            scale: 1.2,
        },
    },
    elementPlus: {
        icon: false,
    },
    pinia: {
        disableVuex: true,
    },
    piniaPersistedstate: {
        cookieOptions: {
            sameSite: 'strict',
            maxAge: 2592000,
        },
    },
    // https://github.com/unocss/unocss/issues/2113
    sourcemap: {
        server: true,
        client: false,
    },
    build: {
        transpile: ['@popperjs'],
    },
    routeRules: {
        '/info/**': { static: true, headers: { 'cache-control': 's-maxage=3600' } },
        '/channel/**': { static: true, headers: { 'cache-control': 's-maxage=3600' } },
        '/': { static: true, headers: { 'cache-control': 's-maxage=3600' } },
        '/channel/**/tag': { static: true, headers: { 'cache-control': 's-maxage=3600' } },
    },
    plugins: [{ src: '~/plugins/baidu-analytics.ts', mode: 'client' }],
    app: {
        head: {
            charset: 'utf-8',
            viewport: 'width=device-width,initial-scale=1.0,user-scalable=no,maximum-scale=1,viewport-fit=cover',
            meta: [
                { name: 'renderer', content: 'webkit' },
                { name: 'applicable-device', content: 'pc,mobile' },
                { 'http-equiv': 'X-UA-Compatible', content: 'IE=edge,chrome=1' },
                { property: 'og:type', content: 'webpage' },
                // <meta name="baidu-site-verification" content="codeva-zC7mXfqCUR" />
                { name: 'baidu-site-verification', content: 'codeva-zC7mXfqCUR' },

                // { 'http-equiv': 'Cache-Control', content: 'no-transform' },
            ],
        },
    },
})
